package at.fhj.pse.datasource.model;

import javax.persistence.Entity;

import java.io.Serializable;

import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Column;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import at.fhj.pse.datasource.exception.DatasourceLayerException;

import java.lang.Override;
import java.util.List;

@Entity
public class Employee implements Serializable {
	
	private static final long serialVersionUID = 1067891362254801905L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id", updatable = false, nullable = false)
	private Long id = null;

	@Column
	private String firstName;

	@Column
	private String lastName;

	@Column(unique = true, nullable = false)
	private long staffNumber;

	@Column(unique = true, nullable = false)
	private String username;

	@Column(nullable = false)
	private String password;


	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		if (department == null) {
			throw new DatasourceLayerException("department is null", new IllegalArgumentException());
		}
		this.department = department;
	}

	@Column
	private boolean isDepartmentManager;

	@Column(nullable = false)
	private float employmentType; // Anstellungsart

	@ManyToOne(fetch=FetchType.LAZY)
	private Department department;

	@OneToMany(mappedBy="employee")
	private List<TimeInterval> timeIntervals;

	public List<TimeInterval> getTimeIntervals() {
		return timeIntervals;
	}

	public void setTimeIntervals(List<TimeInterval> timeIntervals) {
		if (timeIntervals == null) {
			throw new DatasourceLayerException("timeIntervals is null", new IllegalArgumentException());
		}
		this.timeIntervals = timeIntervals;
	}
	
	/*
	private TimeInterval ti;

	public TimeInterval getTimeInterval() {
		return ti;
	}

	public void setTimeInterval(TimeInterval ti) {
		if (ti == null) {
			throw new IllegalArgumentException("timeInterval is null");
		}
		this.ti = ti;
	}
	*/

	public float getEmploymentType() {
		return employmentType;
	}

	public void setEmploymentType(float employmentType) {
		this.employmentType = employmentType;
	}

	public String getPassword() {
		return password;
	}

	public boolean isDepartmentManager() {
		return isDepartmentManager;
	}

	public void setDepartmentManager(boolean isDepartmentManager) {
		this.isDepartmentManager = isDepartmentManager;
	}

	public Long getId() {
		return this.id;
	}

	public void setId(final Long id) {
		this.id = id;
	}

	@Override
	public boolean equals(Object that) {
		if (this == that) {
			return true;
		}
		if (that == null) {
			return false;
		}
		if (getClass() != that.getClass()) {
			return false;
		}
		if (id != null) {
			return id.equals(((Employee) that).id);
		}
		return super.equals(that);
	}

	@Override
	public int hashCode() {
		if (id != null) {
			return id.hashCode();
		}
		return super.hashCode();
	}

	public String getFirstName() {
		return this.firstName;
	}

	public void setFirstName(final String firstName) {
		if (firstName == null) {
			throw new DatasourceLayerException("firstName is null", new IllegalArgumentException());
		}
		this.firstName = firstName;
	}

	public String getLastName() {
		return this.lastName;
	}

	public void setLastName(final String lastName) {
		if (lastName == null) {
			throw new DatasourceLayerException("lastName is null", new IllegalArgumentException());
		}
		this.lastName = lastName;
	}

	public long getStaffNumber() {
		return staffNumber;
	}

	public void setStaffNumber(long staffNumber) {
		this.staffNumber = staffNumber;
	}


	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		if (username == null) {
			throw new DatasourceLayerException("username is null", new IllegalArgumentException());
		}
		this.username = username;
	}

	@Override
	public String toString() {
		return this.username;
	}
}